package com.shanghai.shanghaielectric.UI.setting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.DB.SharepreferenceUtil;
import com.shanghai.shanghaielectric.Model.EventBus.EventbusModel_setting_debug_state;
import com.shanghai.shanghaielectric.Model.WriteDataModel;
import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_Alert;
import com.shanghai.shanghaielectric.UI.KeyBoard.MKeyBoard_InputValue;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import shanghaielectric_lib.Communication.CommunicateManager;
import shanghaielectric_lib.Utils.LogUtil;

//import com.shanghai.shanghaielectric.DB.SharepreferenceUtil;

/**
 * 设置-参数界面
 */
public class SettingParamFragment extends Fragment {
    View settingParamView;
    Context mContext;


    ArrayAdapter<String> spDeviceTypeAdapter;
    ArrayAdapter<String> spWorkingmodeAdapter;
    ArrayAdapter<String> spLoadPositionAdapter;
    ArrayAdapter<String> spLoadDirectioneAdapter;
    ArrayAdapter<String> spCapacitorGroupAdapter;


    String[] spDeviceTypeArrays = new String[]{"单机模式", "多机模式", "有源+无源混合模式"};
    String[] spWorkingmodeArrays = new String[]{"无", "不平衡补偿", "不平衡补偿+无功补偿", "不平衡补偿+无功补偿+谐波补偿", "不平衡补偿+谐波补偿", "不平衡补偿+谐波补偿+无功补偿", "无功补偿", "无功补偿+不平衡补偿", "无功补偿+不平衡补偿+谐波补偿", "无功补偿+谐波补偿", "无功补偿+谐波补偿+不平衡补偿", "谐波补偿", "谐波补偿+不平衡补偿", "谐波补偿+不平衡补偿+无功补偿", "谐波补偿+无功补偿", "谐波补偿+无功补偿+不平衡补偿"};
    String[] spLoadPositionArrays = new String[]{"网侧", "负载侧"};
    String[] spLoadDirectioneArrays = new String[]{"正方向", "反方向"};
    String[] spCapacitorGroupArrays = new String[]{"240kVA", "300kVA", "360kVA"};

    @BindView(R.id.sp_deviceType)
    Spinner spDeviceType;
    @BindView(R.id.sp_workingmode)
    Spinner spWorkingmode;
    @BindView(R.id.sp_loadPosition)
    Spinner spLoadPosition;
    @BindView(R.id.sp_capacitorGroup)
    Spinner spCapacitorGroup;
    @BindView(R.id.sp_loadDirection)
    Spinner spLoadDirection;
    @BindView(R.id.tv_UrmsMax)
    TextView tvUrmsMax;
    @BindView(R.id.tv_powerFactor)
    TextView tvPowerFactor;
    @BindView(R.id.tv_UrmsMin)
    TextView tvUrmsMin;
    @BindView(R.id.tv_capacitor_cutting_delay)
    TextView tvCapacitorCuttingDelay;
    @BindView(R.id.setting_IrmsMax)
    TextView settingIrmsMax;
    @BindView(R.id.tv_hysteresisBandWidth)
    TextView tvHysteresisBandWidth;
    @BindView(R.id.tv_thdLimitU)
    TextView tvThdLimitU;
    @BindView(R.id.tv_switchingInterval)
    TextView tvSwitchingInterval;
    @BindView(R.id.tv_thdLimitI)
    TextView tvThdLimitI;
    @BindView(R.id.tv_ctSystemOrLoad)
    TextView tvCtSystemOrLoad;
    @BindView(R.id.tv_QpresentSet)
    TextView tvQpresentSet;


    @BindView(R.id.harmonic_phase_angle_bias_3)
    TextView harmonicPhaseAngleBias3;
    @BindView(R.id.harmonic_proportion_3)
    TextView harmonicProportion3;
    @BindView(R.id.harmonic_phase_angle_bias_5)
    TextView harmonicPhaseAngleBias5;
    @BindView(R.id.harmonic_proportion_5)
    TextView harmonicProportion5;
    @BindView(R.id.harmonic_phase_angle_bias_7)
    TextView harmonicPhaseAngleBias7;
    @BindView(R.id.harmonic_proportion_7)
    TextView harmonicProportion7;
    @BindView(R.id.harmonic_phase_angle_bias_9)
    TextView harmonicPhaseAngleBias9;
    @BindView(R.id.harmonic_proportion_9)
    TextView harmonicProportion9;
    @BindView(R.id.harmonic_phase_angle_bias_11)
    TextView harmonicPhaseAngleBias11;
    @BindView(R.id.harmonic_proportion_11)
    TextView harmonicProportion11;
    @BindView(R.id.harmonic_phase_angle_bias_13)
    TextView harmonicPhaseAngleBias13;
    @BindView(R.id.harmonic_proportion_13)
    TextView harmonicProportion13;
    @BindView(R.id.harmonic_phase_angle_bias_15)
    TextView harmonicPhaseAngleBias15;
    @BindView(R.id.harmonic_proportion_15)
    TextView harmonicProportion15;
    @BindView(R.id.harmonic_phase_angle_bias_17)
    TextView harmonicPhaseAngleBias17;
    @BindView(R.id.harmonic_proportion_17)
    TextView harmonicProportion17;
    @BindView(R.id.higherHarmonicSelect_1_19)
    Button higherHarmonicSelect119;
    @BindView(R.id.higherHarmonicSelect_1_21)
    Button higherHarmonicSelect121;
    @BindView(R.id.higherHarmonicSelect_1_23)
    Button higherHarmonicSelect123;
    @BindView(R.id.higherHarmonicSelect_1_25)
    Button higherHarmonicSelect125;
    @BindView(R.id.higherHarmonicSelect_1_27)
    Button higherHarmonicSelect127;
    @BindView(R.id.higherHarmonicSelect_1_29)
    Button higherHarmonicSelect129;
    @BindView(R.id.higherHarmonicSelect_1_31)
    Button higherHarmonicSelect131;
    @BindView(R.id.higherHarmonicSelect_1_33)
    Button higherHarmonicSelect133;
    @BindView(R.id.higherHarmonicSelect_1_35)
    Button higherHarmonicSelect135;
    @BindView(R.id.higherHarmonicSelect_1_37)
    Button higherHarmonicSelect137;
    @BindView(R.id.higherHarmonicSelect_1_39)
    Button higherHarmonicSelect139;
    @BindView(R.id.higherHarmonicSelect_1_41)
    Button higherHarmonicSelect141;
    @BindView(R.id.higherHarmonicSelect_1_43)
    Button higherHarmonicSelect143;
    @BindView(R.id.higherHarmonicSelect_1_45)
    Button higherHarmonicSelect145;
    @BindView(R.id.higherHarmonicSelect_1_47)
    Button higherHarmonicSelect147;
    @BindView(R.id.higherHarmonicSelect_1_49)
    Button higherHarmonicSelect149;
    @BindView(R.id.higherHarmonicSelect_2_51)
    Button higherHarmonicSelect251;
    @BindView(R.id.higherHarmonicSelect_2_53)
    Button higherHarmonicSelect253;
    @BindView(R.id.higherHarmonicSelect_2_55)
    Button higherHarmonicSelect255;
    @BindView(R.id.higherHarmonicSelect_2_57)
    Button higherHarmonicSelect257;
    @BindView(R.id.higherHarmonicSelect_2_59)
    Button higherHarmonicSelect259;
    @BindView(R.id.higherHarmonicSelect_2_61)
    Button higherHarmonicSelect261;
    @BindView(R.id.save_parameter)
    Button saveParameter;
    @BindView(R.id.send_parameter)
    Button sendParameter;


    @BindView(R.id.view)
    View view;
    @BindView(R.id.user_scroll)
    RelativeLayout userScroll;
    Unbinder unbinder;

    WriteDataModel writeDataModel_temp;//临时保存界面修改的数据


    WriteDataModel writeDataModel = Constants.writeDataModel;//实时通信数据源

    MKeyBoard_InputValue mKeyBoardInputValue;

    List<Button> buttonList;
    @BindView(R.id.tv_phaseOffset1)
    TextView tvPhaseOffset1;
    @BindView(R.id.tv_ctCabinet)
    TextView tvCtCabinet;
    @BindView(R.id.tv_AMpercent)
    TextView tvAMpercent;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("SettingParam____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        System.out.println("SettingParam____onCreate");
    }

    private void initListener() {
        spDeviceType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mContext, "点击了" + i + "+" + l, Toast.LENGTH_SHORT).show();
                //deviceType:单机：1,   多机 ：2   有源+无源：  3
                writeDataModel_temp.deviceType = i + 1;
                EventbusModel_setting_debug_state eventbusModel_setting_debug_state = new EventbusModel_setting_debug_state();
                eventbusModel_setting_debug_state.tag = "deviceType";
                eventbusModel_setting_debug_state.flag = writeDataModel_temp.deviceType == 3;
                EventBus.getDefault().post(eventbusModel_setting_debug_state);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        spWorkingmode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //device 值：0~15
                writeDataModel_temp.deviceModel = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        final int[] position = {0};
        final int direction[] = {1};// 1 为正方向
        spLoadPosition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                position[0] = i;
                writeDataModel_temp.setCtSystemOrLoadProperty(position[0], direction[0]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spLoadDirection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                direction[0] = i == 0 ? 1 : 0;
                writeDataModel_temp.setCtSystemOrLoadProperty(position[0], direction[0]);

//                Toast.makeText(mContext, "赋值" + direction[0] + "+====" + writeDataModel_temp.getCtSystemOrLoadProperty(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        spCapacitorGroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                writeDataModel_temp.passiveCapacitorGroup = i + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initData() {
        writeDataModel = Constants.writeDataModel;
//        writeDataModel_temp = SharepreferenceUtil.getObject(MyApplication.getContext(), WriteDataModel.class.getName());
        writeDataModel_temp = writeDataModel;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("SettingParam____onCreateView");
        settingParamView = inflater.inflate(R.layout.setting_param_fragment,
                container, false);
        mContext = getActivity().getApplicationContext();
        //隐藏软键盘
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
        //点击其他部位隐藏软键盘
        settingParamView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (getActivity().getCurrentFocus() != null && getActivity().getCurrentFocus().getWindowToken() != null) {
                        manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
        });

        unbinder = ButterKnife.bind(this, settingParamView);
        initView();
        initListener();

        return settingParamView;
    }

    private void initView() {
        spDeviceTypeAdapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, spDeviceTypeArrays);
        spWorkingmodeAdapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, spWorkingmodeArrays);
        spLoadPositionAdapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, spLoadPositionArrays);
        spLoadDirectioneAdapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, spLoadDirectioneArrays);
        spCapacitorGroupAdapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, spCapacitorGroupArrays);
        spDeviceType.setAdapter(spDeviceTypeAdapter);
        spWorkingmode.setAdapter(spWorkingmodeAdapter);
        spLoadPosition.setAdapter(spLoadPositionAdapter);
        spLoadDirection.setAdapter(spLoadDirectioneAdapter);
        spCapacitorGroup.setAdapter(spCapacitorGroupAdapter);
        buttonList = new ArrayList<>();
        buttonList.add(higherHarmonicSelect119);
        buttonList.add(higherHarmonicSelect121);
        buttonList.add(higherHarmonicSelect123);
        buttonList.add(higherHarmonicSelect125);
        buttonList.add(higherHarmonicSelect127);
        buttonList.add(higherHarmonicSelect129);
        buttonList.add(higherHarmonicSelect131);
        buttonList.add(higherHarmonicSelect133);
        buttonList.add(higherHarmonicSelect135);
        buttonList.add(higherHarmonicSelect137);
        buttonList.add(higherHarmonicSelect139);
        buttonList.add(higherHarmonicSelect141);
        buttonList.add(higherHarmonicSelect143);
        buttonList.add(higherHarmonicSelect145);
        buttonList.add(higherHarmonicSelect147);
        buttonList.add(higherHarmonicSelect149);
        buttonList.add(higherHarmonicSelect251);
        buttonList.add(higherHarmonicSelect253);
        buttonList.add(higherHarmonicSelect255);
        buttonList.add(higherHarmonicSelect257);
        buttonList.add(higherHarmonicSelect259);
        buttonList.add(higherHarmonicSelect261);

        spWorkingmode.setSelection(Constants.writeDataModel.deviceModel);
        spDeviceType.setSelection(Constants.writeDataModel.deviceType - 1);

        spLoadPosition.setSelection(Constants.writeDataModel.getCtSystemOrLoadProperty_position());
        spLoadDirection.setSelection(Constants.writeDataModel.getCtSystemOrLoadProperty_direction() == 0 ? 1 : 0);
        spCapacitorGroup.setSelection(Constants.writeDataModel.getPassiveCapacitorGroup());

        tvUrmsMax.setText(String.valueOf(writeDataModel.urmsMax));
        tvUrmsMin.setText(String.valueOf(writeDataModel.urmsMin));
        settingIrmsMax.setText(String.valueOf(writeDataModel.irmsMax));
        tvQpresentSet.setText(String.valueOf(writeDataModel.qPercentSet));
        tvThdLimitU.setText(String.valueOf(writeDataModel.thdLimitU));
        tvThdLimitI.setText(String.valueOf(writeDataModel.thdLimitI));
        tvPowerFactor.setText(String.valueOf(writeDataModel.powerFactorSet));
        tvCapacitorCuttingDelay.setText(String.valueOf(writeDataModel.capacitorSwitchDelay));
        tvHysteresisBandWidth.setText(String.valueOf(writeDataModel.hysteresysBanWidth));
        tvSwitchingInterval.setText(String.valueOf(writeDataModel.switchInterval));
        tvCtSystemOrLoad.setText(String.valueOf(writeDataModel.ctSystemOrLoad));
        tvPhaseOffset1.setText(String.valueOf(writeDataModel.phaseOffset1));
        tvAMpercent.setText(String.valueOf(writeDataModel.amPercent1));
        tvCtCabinet.setText(String.valueOf(writeDataModel.ctCabinet));

        harmonicPhaseAngleBias3.setText(String.valueOf(writeDataModel.phaseOffset3));
        harmonicPhaseAngleBias5.setText(String.valueOf(writeDataModel.phaseOffset5));
        harmonicPhaseAngleBias7.setText(String.valueOf(writeDataModel.phaseOffset7));
        harmonicPhaseAngleBias9.setText(String.valueOf(writeDataModel.phaseOffset9));
        harmonicPhaseAngleBias11.setText(String.valueOf(writeDataModel.phaseOffset11));
        harmonicPhaseAngleBias13.setText(String.valueOf(writeDataModel.phaseOffset13));
        harmonicPhaseAngleBias15.setText(String.valueOf(writeDataModel.phaseOffset15));
        harmonicPhaseAngleBias17.setText(String.valueOf(writeDataModel.phaseOffset17));

        harmonicProportion3.setText(String.valueOf(writeDataModel.amPercent3));
        harmonicProportion5.setText(String.valueOf(writeDataModel.amPercent5));
        harmonicProportion7.setText(String.valueOf(writeDataModel.amPercent7));
        harmonicProportion9.setText(String.valueOf(writeDataModel.amPercent9));
        harmonicProportion11.setText(String.valueOf(writeDataModel.amPercent11));
        harmonicProportion13.setText(String.valueOf(writeDataModel.amPercent13));
        harmonicProportion15.setText(String.valueOf(writeDataModel.amPercent15));
        harmonicProportion17.setText(String.valueOf(writeDataModel.amPercent17));

        for (int i = 0; i < buttonList.size(); i++) {
            if (i < 16) {
                buttonList.get(i).setBackgroundResource(Constants.writeDataModel.getHighHarmonicSelect1(i) ? R.drawable.bt_pressed_red_background_ : R.drawable.btn_default);
            } else {
                buttonList.get(i).setBackgroundResource(Constants.writeDataModel.getHighHarmonicSelect2(i - 16) ? R.drawable.bt_pressed_red_background_ : R.drawable.btn_default);
            }
        }
    }

    private void updateView() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("SettingParam____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("SettingParam____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e("zzzzzzzzzz", "zhixingle");
        Context context = MyApplication.getContext();
//        Constants_modbus.isSettingState=true;
        System.out.println("SettingParam____onResume---------");
    }

    @Override
    public void onPause() {
        super.onPause();


        System.out.println("SettingParam____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("SettingParam____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("SettingParam____onDestroyView");
        unbinder.unbind();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        System.out.println("SettingParam____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("SettingParam____onDetach");
        LogUtil.e("ggggggggg", "点击了");
    }


    @OnClick({R.id.tv_QpresentSet, R.id.tv_UrmsMax, R.id.tv_powerFactor, R.id.tv_UrmsMin, R.id.tv_capacitor_cutting_delay, R.id.setting_IrmsMax, R.id.tv_hysteresisBandWidth, R.id.tv_thdLimitU, R.id.tv_switchingInterval, R.id.tv_thdLimitI, R.id.tv_ctSystemOrLoad, R.id.harmonic_phase_angle_bias_3, R.id.harmonic_proportion_3, R.id.harmonic_phase_angle_bias_5, R.id.harmonic_proportion_5, R.id.harmonic_phase_angle_bias_7, R.id.harmonic_proportion_7, R.id.harmonic_phase_angle_bias_9, R.id.harmonic_proportion_9, R.id.harmonic_phase_angle_bias_11, R.id.harmonic_proportion_11, R.id.harmonic_phase_angle_bias_13, R.id.harmonic_proportion_13, R.id.harmonic_phase_angle_bias_15, R.id.harmonic_proportion_15, R.id.harmonic_phase_angle_bias_17, R.id.harmonic_proportion_17, R.id.higherHarmonicSelect_1_19, R.id.higherHarmonicSelect_1_21, R.id.higherHarmonicSelect_1_23, R.id.higherHarmonicSelect_1_25, R.id.higherHarmonicSelect_1_27, R.id.higherHarmonicSelect_1_29, R.id.higherHarmonicSelect_1_31, R.id.higherHarmonicSelect_1_33, R.id.higherHarmonicSelect_1_35, R.id.higherHarmonicSelect_1_37, R.id.higherHarmonicSelect_1_39, R.id.higherHarmonicSelect_1_41, R.id.higherHarmonicSelect_1_43, R.id.higherHarmonicSelect_1_45, R.id.higherHarmonicSelect_1_47, R.id.higherHarmonicSelect_1_49, R.id.higherHarmonicSelect_2_51, R.id.higherHarmonicSelect_2_53, R.id.higherHarmonicSelect_2_55, R.id.higherHarmonicSelect_2_57, R.id.higherHarmonicSelect_2_59, R.id.higherHarmonicSelect_2_61, R.id.save_parameter, R.id.send_parameter, R.id.tv_phaseOffset1, R.id.tv_AMpercent, R.id.tv_ctCabinet})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_UrmsMax:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(235, 275);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.urmsMax = value;
                        tvUrmsMax.setText(String.valueOf(value));
                    }
                });
                break;
            case R.id.tv_powerFactor:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 100);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.powerFactorSet = value;
                        tvPowerFactor.setText(String.valueOf(value));
                    }
                });
                break;
            case R.id.tv_UrmsMin:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(165, 198);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.urmsMin = value;
                        tvUrmsMin.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_capacitor_cutting_delay:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 10000);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.capacitorSwitchDelay = value;
                        tvCapacitorCuttingDelay.setText(String.valueOf(value));
                    }
                });
                break;
            case R.id.setting_IrmsMax:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(110, 160);//%
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.irmsMax = value;
                        settingIrmsMax.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_hysteresisBandWidth:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(1, 100);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.hysteresysBanWidth = value;
                        tvHysteresisBandWidth.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_thdLimitU:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(10, 30);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.thdLimitU = value;
                        tvThdLimitU.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_switchingInterval:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 10000);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.switchInterval = value;
                        tvSwitchingInterval.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_thdLimitI:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(10, 30);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.thdLimitI = value;
                        tvThdLimitI.setText(String.valueOf(value));

                    }
                });
                break;

            case R.id.tv_ctSystemOrLoad:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(10, 5000);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.ctSystemOrLoad = value;
                        tvCtSystemOrLoad.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_QpresentSet:

                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 100);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.qPercentSet = value;
                        tvQpresentSet.setText(String.valueOf(value));
                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_3:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset3 = value;
                        harmonicPhaseAngleBias3.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_3:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent3 = value;
                        harmonicProportion3.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_5:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset5 = value;
                        harmonicPhaseAngleBias5.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_5:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent5 = value;
                        harmonicProportion5.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_7:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset7 = value;
                        harmonicPhaseAngleBias7.setText(String.valueOf(value));
                        harmonicPhaseAngleBias7.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_7:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent7 = value;
                        harmonicProportion7.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_9:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset9 = value;
                        harmonicPhaseAngleBias9.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_9:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent9 = value;
                        harmonicProportion9.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_11:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset11 = value;
                        harmonicPhaseAngleBias11.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_11:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent11 = value;
                        harmonicProportion11.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_13:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset13 = value;
                        harmonicPhaseAngleBias13.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_13:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent13 = value;
                        harmonicPhaseAngleBias3.setText(String.valueOf(value));
                        harmonicProportion13.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_15:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset15 = value;
                        harmonicPhaseAngleBias15.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_15:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent15 = value;
                        harmonicProportion15.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_phase_angle_bias_17:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset17 = value;
                        harmonicPhaseAngleBias17.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.harmonic_proportion_17:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 120);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent17 = value;
                        harmonicProportion17.setText(String.valueOf(value));

                    }
                });
                break;

            case R.id.tv_phaseOffset1:

                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(-512, 512);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.phaseOffset1 = value;
                        tvPhaseOffset1.setText(String.valueOf(value));

                    }
                });
                break;

//                todo 临时值  开发文档是  0~120
            case R.id.tv_AMpercent:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(0, 180);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.amPercent1 = value;
                        tvAMpercent.setText(String.valueOf(value));

                    }
                });
                break;
            case R.id.tv_ctCabinet:
                mKeyBoardInputValue = new MKeyBoard_InputValue();
                mKeyBoardInputValue.setInputValueRange(10, 1000);
                mKeyBoardInputValue.show(getFragmentManager(), "");
                mKeyBoardInputValue.setMkeyBoardListener(new MKeyBoard_InputValue.MkeyBoardListener() {
                    @Override
                    public void onClink(int value) {
                        writeDataModel_temp.ctCabinet = value;
                        tvCtCabinet.setText(String.valueOf(value));
                    }
                });
                break;
            case R.id.save_parameter:
//弹窗，其实是否保存数据--
                showDiolg("是否保存参数？");
                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
//                            Constants.writeDataModel = writeDataModel_temp;//writeDataModel_temp中 第一帧数据为空，因此只赋值其他数据
                            copyDataToConstant_Writedate(writeDataModel_temp);

                            //参数保存到本地：
                            Toast.makeText(mContext, "参数保存成功" + flag, Toast.LENGTH_SHORT).show();
                            SharepreferenceUtil.saveObject(MyApplication.getContext(), WriteDataModel.class.getName(), writeDataModel_temp);
                        } else {
                            Toast.makeText(mContext, "取消保存，离开页面后，数据将恢复历史状态" + flag, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.send_parameter:
//弹窗提示是否下发数据
                showDiolg("是否下发参数？");
                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
                            Toast.makeText(mContext, "正在下发参数..." + flag, Toast.LENGTH_SHORT).show();
                            //下发参数：----
                            CommunicateManager.sendData_all();
                        } else {
                            Toast.makeText(mContext, "下发参数取消..." + flag, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
        for (int i = 0; i < buttonList.size(); i++) {
            if (view.getId() == buttonList.get(i).getId()) {
                if (i < 16) {
                    writeDataModel_temp.setHighHarmonicSelect1(i, !writeDataModel_temp.getHighHarmonicSelect1(i));
                    buttonList.get(i).setBackgroundResource(writeDataModel_temp.getHighHarmonicSelect1(i) ? R.drawable.bt_pressed_red_background_ : R.drawable.btn_default);
                } else {
                    writeDataModel_temp.setHighHarmonicSelect2(i - 16, !writeDataModel_temp.getHighHarmonicSelect2(i - 16));
                    buttonList.get(i).setBackgroundResource(writeDataModel_temp.getHighHarmonicSelect2(i - 16) ? R.drawable.bt_pressed_red_background_ : R.drawable.btn_default);
                }
            }
        }
    }

    private void showDiolg(String commandName) {
        Dialog_Alert diolgAlert = new Dialog_Alert();
        diolgAlert.setCommandName(commandName);
        diolgAlert.show(this.getFragmentManager(), "");
    }

    private void copyDataToConstant_Writedate(WriteDataModel resource) {
        int unitEnable = Constants.writeDataModel.unitEnable;
        int totalCapacity = Constants.writeDataModel.totalCapacity;
        int testCapacitor1 = Constants.writeDataModel.testCapacitor1;
        Constants.writeDataModel = resource;
        Constants.writeDataModel.unitEnable = unitEnable;
        Constants.writeDataModel.totalCapacity = totalCapacity;
        Constants.writeDataModel.testCapacitor1 = testCapacitor1;

    }
}
